<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\balancediscount\model;

class CardWater extends \app\BaseModel
{
    protected $name = "massage_balance_discount_card_water";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("top desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function updateCash($uniacid, $card_id, $cash, $is_add, $order_id, $type, $user_id, $refund_id = 0)
    {
        $is_add = $is_add == 1 ? $is_add : -1;
        $card_model = new UserCard();
        $card_info = $card_model->dataInfo(["id" => $card_id]);
        if(empty($card_info)) {
            return true;
        }
        $last_record = $this->where(["card_id" => $card_id])->order("id desc")->find();
        $card_cash = $card_info["cash"];
        if(!empty($last_record) && round($last_record->after_cash, 2) != round($card_cash, 2)) {
            return 0;
        }
        if($is_add == -1 && $card_cash < $cash) {
            return 0;
        }
        $true_cash = $is_add == 1 ? $cash : $cash * -1;
        if($true_cash == 0) {
            return true;
        }
        $res = $card_model->where(["id" => $card_id, "cash" => $card_cash])->update(["cash" => \think\facade\Db::Raw("cash+" . $true_cash)]);
        if($res == 0) {
            return 0;
        }
        $insert = ["uniacid" => $uniacid, "card_id" => $card_id, "user_id" => $user_id, "before_cash" => $card_cash, "after_cash" => $card_cash + $true_cash, "cash" => $cash, "res" => $res, "create_time" => time(), "add" => $is_add, "order_id" => $order_id, "type" => $type, "refund_id" => $refund_id];
        $res = $this->insert($insert);
        if($res == 0) {
            return 0;
        }
        return $res;
    }
}

?>